<template>
  <div class="container">
    <el-dialog
      :close-on-click-modal="false"
      :modal="false"
      :visible.sync="isShowDialog"
      class="thisblack-bgc"
      width="600px"
    >
      <span slot="title">
        <p class="head-title">修改超级管理员</p>
      </span>
      <div class="content">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="175px">
          <el-form-item label="超级管理员手机号" prop="superMobile">
            <el-input
              v-model="ruleForm.superMobile"
              class="w300"
              disabled
              size="small"
            />
          </el-form-item>
          <el-form-item v-if="isCodeChange" :prop="isCodeChange ? 'superCode' : false" label="验证码">
            <el-input
              v-model="ruleForm.superCode"
              class="w175 mr16"
              placeholder="请输入验证码"
              size="small"
            />
            <el-button v-if="timeShow" class="w110" plain @click="getSendCode">获取验证码</el-button>
            <el-button v-if="!timeShow" class="w110" plain>重新获取({{ count }}秒)</el-button>
          </el-form-item>
          <el-form-item v-if="!isCodeChange" :prop="isCodeChange ? false : 'imShowId'" label="展位号">
            <el-input
              v-model="ruleForm.imShowId"
              class="w300 mr16"
              disabled
              size="small"
            />
          </el-form-item>
          <el-form-item v-if="!isCodeChange" :prop="isCodeChange ? false : 'imPwd'" label="展位密码">
            <el-input
              v-model="ruleForm.imPwd"
              class="w300 mr16"
              placeholder="请输入展位密码"
              size="small"
            />
          </el-form-item>
          <p v-if="isCodeChange" class="fromPtitle" @click="getCodeChange">无法联系到超管，点击此处使用展位号+密码更换。</p>
          <p v-if="!isCodeChange" class="fromPtitle" @click="getCodeChange">不知道密码，点击此处通过超管手机验证码更换。</p>
          <el-form-item label="原有超管更换为" prop="roleId">
            <el-select v-model="ruleForm.roleId" class="w300" placeholder="请选择角色" size="small">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              >
                {{ item.roleName }}
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="新管理员手机号" prop="updateMobile">
            <el-input
              v-model="ruleForm.updateMobile"
              class="w300"
              placeholder="请输入新管理员手机号"
              size="small"
            />
          </el-form-item>
          <el-form-item label="验证码" prop="updateCode">
            <el-input
              v-model="ruleForm.updateCode"
              class="w175 mr16"
              placeholder="请输入验证码"
              size="small"
            />
            <el-button v-if="newTimeShow" class="w110" plain @click="getNewSendCode('ruleForm')">获取验证码</el-button>
            <el-button v-if="!newTimeShow" class="w110" plain>重新获取({{ NewCount }}秒)</el-button>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 更换成功弹窗 -->
    <el-dialog
      :close-on-click-modal="false"
      :modal="false"
      :show-close="false"
      :visible.sync="isShowLoginDialog"
      class="thisblack-bgc"
      width="600px"
    >
      <span slot="title">
        <p class="head-title">提示</p>
      </span>
      <div class="content">
        <p class="p-tip">您的角色权限发生变化，请重新登录。</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="islogoutDialog" type="primary" @click="logout">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 更换不成功 展位号不足 -->
    <el-dialog
      :close-on-click-modal="false"
      :modal="false"
      :show-close="false"
      :visible.sync="isShowBoothDialog"
      class="thisblack-bgc"
      width="600px"
    >
      <span slot="title">
        <p class="head-title">提示</p>
      </span>
      <div class="content">
        <p class="p-error">更换超级管理员不成功</p>
        <p class="p-error-tip">贵展位账号数已用完，请将现有成员设置超级管理员</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import api from '@/api'
import { validateMobile } from '@/utils/validate'
import { getImShowId } from '@/utils/auth'

export default {
  name: 'SuperAdmin',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    boothname: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      isShowLoginDialog: false,
      islogoutDialog: false,
      isShowBoothDialog: false,
      loading: false,
      isCodeChange: true, // 是否是验证码更换管理员
      ruleForm: {
        superMobile: '',
        superCode: '',
        roleId: null,
        updateMobile: '',
        updateCode: '',
        imShowId: '', // 展位号
        imPwd: '', // 展位密码
        type: 1 // 更换类型：1验证码  2展位号密码
      },
      rules: {
        superMobile: [{ required: true, validator: validateMobile, trigger: 'blur' }],
        superCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        roleId: [{ required: true, message: '请选择角色', trigger: 'change' }],
        updateMobile: [{ required: true, validator: validateMobile, trigger: 'blur' }],
        updateCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        imShowId: [{ required: true, message: '请输入展位号', trigger: 'blur' }],
        imPwd: [{ required: true, message: '请输入展位密码', trigger: 'blur' }]
      },
      roleList: [],
      timeShow: true,
      count: '',
      timer: null,
      newTimeShow: true,
      NewCount: '',
      newTimer: null
    }
  },
  computed: {
    isShowDialog: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('closeEvents', 'isSuperAdmin', val)
      }
    }
  },
  created () {
    this.getQueryAccountRole()
    this.ruleForm.superMobile = this.boothname.immobile || this.boothname.mobile
    this.ruleForm.type = this.isCodeChange ? 1 : 2
  },
  methods: {
    closeDialog () {
      this.$emit('closeEvents', 'isSuperAdmin', false)
    },
    // 获取权限列表
    async getQueryAccountRole () {
      const { data } = await api.imlist.queryAccountRoleList()
      this.roleList = data.filter(item => {
        return item.roleId !== -1
      })
      this.roleList.unshift({
        roleId: '-2',
        roleName: '删除此账号'
      })
    },
    async getSendCode () {
      const params = {
        mobile: this.ruleForm.superMobile,
        type: 1 // 0-快速注册登录时，1-验证码原手机号时，2-修改密码时，3-绑定手机号
      }
      await api.imlist.getUserCode(params)
      const TIME_COUNT = 60
      if (!this.timer) {
        this.count = TIME_COUNT
        this.timeShow = false
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--
          } else {
            this.timeShow = true
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      }
    },
    getNewSendCode (formName) {
      this.$refs[formName].validateField(['updateMobile'], async (valid) => {
        if (valid === '') {
          const params = {
            mobile: this.ruleForm.updateMobile,
            type: 3 // 0-快速注册登录时，1-验证码原手机号时，2-修改密码时，3-绑定手机号
          }
          await api.imlist.getUserCode(params)
          const TIME_COUNT = 60
          if (!this.newTimer) {
            this.NewCount = TIME_COUNT
            this.newTimeShow = false
            this.newTimer = setInterval(() => {
              if (this.NewCount > 0 && this.NewCount <= TIME_COUNT) {
                this.NewCount--
              } else {
                this.newTimeShow = true
                clearInterval(this.newTimer)
                this.newTimer = null
              }
            }, 1000)
          }
        } else {
          return false
        }
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            await api.imlist.updateSuperBossMobile(this.ruleForm)
            this.loading = false
            this.isShowLoginDialog = true
          } catch (error) {
            this.loading = false
          }
        } else {
          return false
        }
      })
    },
    async logout () {
      this.islogoutDialog = true
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?type=sign&redirect=${this.$route.query.redirect}`)
      this.islogoutDialog = false
    },
    getCodeChange () {
      this.ruleForm.imShowId = getImShowId()
      this.$refs.ruleForm.resetFields()
      this.isCodeChange = !this.isCodeChange
      this.ruleForm.type = this.isCodeChange ? 1 : 2
    }
  }
}
</script>
<style lang="scss" scoped>
.w300 {
  width: 300px!important;
}
.w175 {
  width: 175px!important;
}
.container {
  .thisblack-bgc {
    background: rgba(0, 0, 0, .5);
  }

  ::v-deep .el-dialog {
    .el-dialog__header {
      padding: 12px 16px;
      background: #F5F8FC;

      .head-title {
        font-size: 16px;
        color: #202933;
        font-weight: 500;
        line-height: 1.5;
      }

      .el-dialog__headerbtn {
        top: 15px;
      }

      .head-tip {
        font-size: 14px;
        color: #B9C0C7;
        font-weight: 400;
        line-height: 1.5;

        a {
          cursor: pointer;
          color: #0C7FF2;
        }
      }
    }

    .el-dialog__body {
      .content {
        .p-tip {
          font-size: 16px;
          color: #202933;
          margin: 20px auto;
          text-align: center;
        }

        .p-error {
          font-size: 16px;
          color: #202933;
          margin: 8px auto;
          text-align: center;
        }

        .p-error-tip {
          font-size: 12px;
          color: #7E8790;
          margin: 8px auto 5px;
          text-align: center;
        }

        .el-form {
          .el-form-item__label {
            color: #202933;
          }

          .el-form-item__content {
            .el-button {
              padding: 7.5px 0;
              background: rgba(12, 127, 242, 0.1);
              font-size: 14px;
              color: #0C7FF2;
              border: 1px solid rgba(12, 127, 242, 0.1);
            }
          }
        }
      }
    }

    .el-dialog__footer {
      padding: 24px 0;
      text-align: center;
      border-top: 1px solid #E5EAF0;
    }
  }

  .fromPtitle {
    width: 100%;
    padding-left: 175px;
    margin-bottom: 13px;
    margin-top: -6px;
    font-size: 13px;
    color: #0C7FF2;
    cursor: pointer;
  }
}
</style>
